package com.demo.controller;

import com.demo.common.Result;
import com.demo.entity.riot.MatchOverview;
import com.demo.entity.riot.MatchTeam;
import com.demo.entity.riot.RankEntry;
import com.demo.entity.riot.Summoner;
import com.demo.exception.ApiException;
import com.demo.service.riot.RiotMatchService;
import com.demo.service.riot.SummonerService;
import com.demo.service.riot.RiotRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/riot")
public class RiotController {

    @Resource
    private final RiotRankService riotRankService;

    @Resource
    private final SummonerService summonerService;
    @Autowired
    private RiotMatchService riotMatchService;

    public RiotController(RiotRankService riotRankService, SummonerService summonerService) {
        this.riotRankService = riotRankService;
        this.summonerService = summonerService;
    }

    /**
     * @param serverRegion 区服
     * @param gameName 游戏id
     * @param tagLine id后缀
     * @return
     */
    @GetMapping("/summoner/account/{serverRegion}/{gameName}/{tagLine}")
    public Result getSummoner(@PathVariable String serverRegion,
                              @PathVariable String gameName,
                              @PathVariable String tagLine) throws ApiException {
        Summoner summoner;
        summoner = summonerService.getSummonerByName(serverRegion, gameName, tagLine);
        return Result.success(summoner);
    }

    /**
     * 返回上次更新是多少小时前
     * @param serverRegion 区服
     * @param puuid 玩家uuid
     * @return 小时数
     */
    @GetMapping("/summoner/account_update_time/{serverRegion}/{puuid}")
    public Result getSummonerUpdateDuration(@PathVariable String serverRegion, @PathVariable String puuid) {
        long duration = summonerService.getUpdateDuration(serverRegion, puuid);
        return Result.success(duration);
    }

    /**
     *
     * @param serverRegion 区服
     * @param puuid 玩家uuid
     * @return 无
     */
    @PutMapping("/summoner/update/{serverRegion}/{puuid}")
    public Result updateSummoner(@PathVariable String serverRegion, @PathVariable String puuid) throws ApiException {
        summonerService.updateSummoner(serverRegion, puuid);
        return Result.success();
    }

    @GetMapping("/rank/info/{serverRegion}/{puuid}")
    public Result getRankInfo(@PathVariable String serverRegion, @PathVariable String puuid) throws ApiException {
        List<RankEntry> list = riotRankService.getRankInfo(serverRegion, puuid);
        return Result.success(list);
    }

    @GetMapping("/rank/update_time/{serverRegion}/{puuid}")
    public Result getRankUpdateDuration(@PathVariable String serverRegion, @PathVariable String puuid) {
        long duration = riotRankService.getUpdateDuration(serverRegion, puuid);
        return Result.success(duration);
    }

    /**
     *
     * @param serverRegion
     * @param puuid
     * @return
     */
    @PutMapping("/rank/update/{serverRegion}/{puuid}")
    public Result updateRankInfo(@PathVariable String serverRegion, @PathVariable String puuid) throws ApiException {
        riotRankService.updateRankEntries(serverRegion, puuid);
        return Result.success();
    }

    @GetMapping("/match/overviews/{serverRegion}/{puuid}")
    public Result getMatchOverviews(@PathVariable String serverRegion, @PathVariable String puuid) throws ApiException, IOException {
        List<MatchOverview> matchOverviews = riotMatchService.getMatches(serverRegion, puuid, false);
        return Result.success(matchOverviews);
    }

//    @GetMapping("/match/matches/{serverRegion}/{puuid}")
//    public Result getMatchIds(@PathVariable String serverRegion, @PathVariable String puuid) throws ApiException {
//        List<MatchInfo> matchinfos = riotMatchService.getMatchesByApi(serverRegion, puuid);
//        return Result.success(matchinfos);
//    }
}
